package com.example.application.task;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeatherTask1_2 implements Runnable{
    private final String TAG="WeatherTask1_2";
    private Handler handler;
    public Handler getHandler() {
        return handler;
    }
    public void setHandler(Handler handler) {
        this.handler = handler;
    }
    @Override
    public void run() {
        Log.i(TAG, "run:run()....");
        List<String> relist1=new ArrayList<String>();
        List<HashMap<String,String>> relist=new ArrayList<HashMap<String,String>>();
        try {
            Document doc = Jsoup.connect("https://tianqi.so.com/weather/101270101").get();
            Elements a0 = doc.getElementsByClass("tip-item");
            Log.i(TAG, "run: "+a0);
            for(int i=0;i<9;i++){
                String title = a0.get(i).getElementsByClass("tip-title").first().text();
                String tip = a0.get(i).getElementsByClass("tip-cont").first().toString().split("title=\"")[1].split("\">")[0];
                relist1.add(title);
                relist1.add(tip);
            }
            for(int i=0;i<relist1.size();i=i+2){
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("type",relist1.get(i));
                map.put("tip", relist1.get(i+1));
                relist.add(map);
            }
            Log.i(TAG, "run: "+relist);
        }catch (IOException e) {
            e.printStackTrace();
        }
        Message msg = handler.obtainMessage(5,relist);
        handler.sendMessage(msg);
        Log.i(TAG, "run:sendMessage");
    }
}
